package cn.edu.henu.team.ui_test;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;

import cn.edu.henu.team.R;
import cn.edu.henu.team.table.examinations;
import cn.edu.henu.team.table.examinationsUtils;

public class ExamActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ExamActivity";
    private static final String KEY_INDEX = "index";

    private TextView tvQue;
    private RadioGroup rgOption;
    private RadioButton rbOption1, rbOption2;
    private TextView tvPre;
    private TextView tvNext;

    private List<examinations> questionArr = examinationsUtils.loadAll();

    private int currentIndex = 0;
    private boolean flag = false;
    private boolean[] flagArr = new boolean[questionArr.size()];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam);

        //取出销毁状态前保存的数据
        if (savedInstanceState != null) {
            currentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        }
        Log.d(TAG, "************************** " + questionArr.size());

        initView();
        updateQuestion();
    }

    //保存销毁前的状态
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_INDEX, currentIndex);
    }

    private void initView() {

        tvQue =  findViewById(R.id.tv_exam_question);
        tvQue.setOnClickListener(this);

        rgOption = findViewById(R.id.rg_exam_choose);
        rbOption1 =  findViewById(R.id.rb_exam_choose1);
        rbOption2 =  findViewById(R.id.rb_exam_choose2);

        rgOption.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_exam_choose1:
                            flag = true;
                        break;
                    case R.id.rb_exam_choose2:
                            flag = false;
                        break;
                }
            }
        });


        tvPre =  findViewById(R.id.tv_exam_previous);
        tvPre.setOnClickListener(this);

        tvNext =  findViewById(R.id.tv_exam_next);
        tvNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_exam_question:
            case R.id.tv_exam_next:
                // 为数组中元素赋值
                flagArr[currentIndex] = flag;
                if (currentIndex == questionArr.size() - 1) {
                    Intent intent = new Intent(ExamActivity.this, ResultActivity.class);
                    intent.putExtra("totalNum", questionArr.size());
                    intent.putExtra("flag", flagArr);
                    startActivity(intent);
                    finish();
                } else {
                    currentIndex++;
                    updateQuestion();
                }
                break;
            case R.id.tv_exam_previous:
                // 为数组中元素赋值
                flagArr[currentIndex] = flag;
                currentIndex--;
                updateQuestion();
                break;
            default:
                break;
        }
    }

    private void updateQuestion() {
        Log.d(TAG, "Updating question text for question and option#************ " + currentIndex);

        tvQue.setText(questionArr.get(currentIndex).getQuestion());
        rbOption1.setText(questionArr.get(currentIndex).getItemA());
        rbOption2.setText(questionArr.get(currentIndex).getItemB());

        rgOption.clearCheck(); // 清除选中状态
        flag = false;

        if (currentIndex == 0) {
            tvPre.setText(R.string.no);
            tvPre.setEnabled(false);
        } else {
            tvPre.setText(R.string.previous);
            tvPre.setEnabled(true);
        }

        if (currentIndex == questionArr.size() -1){
            tvNext.setText(R.string.done);
        }else{
            tvNext.setText(R.string.next);
        }
    }
}
